package cn.lanqiao.bigDate.service.sys.impl;

import cn.lanqiao.bigDate.entity.User;
import cn.lanqiao.bigDate.entity.sys.SysDept;
import cn.lanqiao.bigDate.mapper.sys.SysDeptMapper;
import cn.lanqiao.bigDate.mapper.sys.UserMapper;
import cn.lanqiao.bigDate.service.sys.SysDeptService;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class SysDeptServiceImpl implements SysDeptService {

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Override
    public IPage<SysDept> findByPage(Page<SysDept> page,String searchDeptName,String searchStatus){
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.like("dept_name",searchDeptName);
        wrapper.like("status",searchStatus);
        Page<SysDept> sysDeptPage = sysDeptMapper.selectPage(page, wrapper);
        return sysDeptPage;

    }

    @Override
    public SysDept findSysDeptOne(Long id) {
        //创建一个QueryWrapper的对象
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysDept sysDept = sysDeptMapper.selectOne(wrapper);
        return sysDept;
    }

    @Override
    public Boolean updateSysDept(SysDept sysDept) {
        if (sysDept != null && sysDept.getId()!=null) {
            Long id = sysDept.getId();
//			查询源对象
            SysDept sysDeptOne = findSysDeptOne(id);
            sysDeptOne.setUpdateTime(LocalDateTime.now());
            sysDeptOne.setId(sysDept.getId());
            sysDeptOne.setParentId(sysDept.getParentId());
            sysDeptOne.setDeptName(sysDept.getDeptName());
            sysDeptOne.setOrderNum(sysDept.getOrderNum());
            sysDeptOne.setStatus(sysDept.getStatus());
            sysDeptOne.setCreateBy(sysDept.getCreateBy());
            sysDeptOne.setUpdateBy(sysDept.getUpdateBy());
            int i = sysDeptMapper.updateById(sysDeptOne);
            if(i==1){
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public Boolean saveSysDept(SysDept sysDept) {
        sysDept.setUpdateTime(LocalDateTime.now());
        sysDept.setCreateTime(LocalDateTime.now());
        int insert = sysDeptMapper.insert(sysDept);
        if(insert==1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteSysDept(String ids) {
        //判断字符长是否为空
        if(!StringUtils.isEmpty(ids)){
            String[] id = ids.split(",");
            List<String> strings = Arrays.asList(id); //将数组转换成列表
            int i = sysDeptMapper.deleteBatchIds(strings);
            if(i>0){
                return true;
            }
        }
        return false;
    }
}
